package com.leetcode.algorithm.y22.m09.w4;

import com.leetcode.algorithm.common.TreeNode;

/**
 * 114. 二叉树展开为链表
 * 
 * https://leetcode.cn/problems/flatten-binary-tree-to-linked-list/
 * 
 * @author jie.deng
 *
 */
class Question0114Solution01 {

	public void flatten(TreeNode root) {
		if (root == null) {
			return;
		}

		TreeNode left = root.left;
		TreeNode right = root.right;
		flatten(left);
		flatten(right);

		root.left = null;
		root.right = left;
		TreeNode cur = root;
		while (cur.right != null) {
			cur = cur.right;
		}
		cur.right = right;
	}

}